package com.lanou.sfu.songforu.fragment;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.lanou.sfu.songforu.R;
import com.lanou.sfu.songforu.adapter.RecommendViewAdapter;
import com.lanou.sfu.songforu.base.BaseFragment;
import com.lanou.sfu.songforu.constant.Url;
import com.lanou.sfu.songforu.datas.RecommendPlayDetailDatas;
import com.lanou.sfu.songforu.singleton.VolleySingleton;

import java.util.List;

import it.sephiroth.android.library.picasso.Picasso;

/**
 * Created by quyan on 15/10/24.
 * 轮播图内容
 */
public class RecommendViewFragment extends BaseFragment implements View.OnClickListener {
    private String code,title;
    private RecommendPlayDetailDatas detailDatas;
    private VolleySingleton volleySingleton;
    private TextView titleSongText,tagText,singNum,collecNum,descText,songCountHostNum;
    private ImageView backImage;
    private CollapsingToolbarLayout collapsing_toolbar;
    private ListView listView;
    private List<RecommendPlayDetailDatas.ContentEntity> datas;
    private RecommendViewAdapter viewAdapter;
    private Toolbar toolbar;
    private ProgressDialog dialog;
    private Context context;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_viewpager, null);
        toolbar = (Toolbar) view.findViewById(R.id.toolbar);
        tagText = (TextView) view.findViewById(R.id.tagText);
        titleSongText = (TextView) view.findViewById(R.id.titleSongText);
        singNum = (TextView) view.findViewById(R.id.singNum);
        collecNum = (TextView) view.findViewById(R.id.collecNum);
        descText = (TextView) view.findViewById(R.id.descText);
        backImage = (ImageView) view.findViewById(R.id.backImag);
        songCountHostNum = (TextView) view.findViewById(R.id.songCountHostNum);
        return view;
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        collapsing_toolbar = (CollapsingToolbarLayout) getActivity().findViewById(R.id.collapsing_toolbar);
        initView();
        Bundle bundle = getArguments();
        code = bundle.getString("ID");
        title = bundle.getString("title");
        toolbar.setNavigationIcon(R.mipmap.bt_return_nor);//返回按钮
        toolbar.setNavigationOnClickListener(this);
        volleySingleton = VolleySingleton.getInstance(getActivity());
        initData();
    }

    public void initView(){
        //设置工具栏标题
        CollapsingToolbarLayout collapsingToolbar = (CollapsingToolbarLayout) getActivity().findViewById(R.id.collapsing_toolbar);
        collapsingToolbar.setContentScrimColor(0xff526bf1);//标题栏颜色
        collapsingToolbar.setCollapsedTitleTextColor(0xffffffff);//字体颜色
        collapsingToolbar.setExpandedTitleColor(0x00000000);//透明度 只在标题栏上显示
        //listview
        listView = (ListView) getActivity().findViewById(R.id.listViewPlay);


    }
    public void initData(){
        String url = Url.URL_PLAY + code;
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                detailDatas = JSON.parseObject(response, RecommendPlayDetailDatas.class);
                //获得专辑信息
                tagText.setText(detailDatas.getTag());
                titleSongText.setText(detailDatas.getTitle());
                singNum.setText(detailDatas.getListenum());
                collecNum.setText(detailDatas.getCollectnum());
                descText.setText(detailDatas.getDesc());
                collapsing_toolbar.setTitle(detailDatas.getTitle());
                Picasso.with(getActivity()).load(detailDatas.getPic_300()).into(backImage);
                if(detailDatas.getContent().size() > 0 ) {
                    songCountHostNum.setText(detailDatas.getContent().size() + "");
                }else {
                    songCountHostNum.setText(0 + "");
                }
                Log.i("cxhab",detailDatas.getPic_500() + "");
                //获取歌曲信息
                datas = detailDatas.getContent();
                viewAdapter = new RecommendViewAdapter(getActivity(),datas);
                listView.setAdapter(viewAdapter);

                if (dialog.isShowing()){
                    dialog.dismiss();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        dialog = new ProgressDialog(context);
        dialog.setMessage(context.getString(R.string.loading));
        dialog.show();
        volleySingleton.addQueue(request);
    }

    @Override
    public void onClick(View v) {
        getFragmentManager().popBackStack();
    }
}

